package com.jdkreader.concurrency.queue;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Toaster implements Runnable{

    private ToastQueue toastQueue;

    private Random random = new Random(47);

    private int count = 0;

    public Toaster(ToastQueue tq) {
        toastQueue = tq;
    }

    @Override
    public void run() {
        try{
            while(!Thread.interrupted()){
                //烹饪吐司
                TimeUnit.MILLISECONDS.sleep(
                        100 + random.nextInt(500));

                Toast toast = new Toast(count++);
                System.out.println(toast);
                toastQueue.put(toast);

            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("Toaster off");
    }
}
